package cn.dada.dadarecipes.base.file;


import cn.dada.dadarecipes.base.response.JsonResult;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@Slf4j
@Api(tags = "文件上传")
@RestController
@RequestMapping("/v1/file/")
public class UpLoadController {
    // 获取文件保存基础路径
    private static String dirPath = "C:/profile/upload/";
//    static final String dirPath = new File("").getAbsolutePath().replace("\\", "/")+"/dada-Recipes/src/main/resources/static";
//    static final String imgPath = dirPath + "/profile/upload/";


    /**
     * 图片上传
     * @param file 此名字必须和前端上传控件name一致
     * @return
     */
    @PostMapping("upload")
    public JsonResult upload(MultipartFile file) throws IOException {
        log.debug("图片上传控制器:{}",file);
        // 获取文件名称
        String filename = file.getOriginalFilename();
        // 获取文件后缀
        /* 可以做文件后缀判断 */
        String suffix = filename.substring(filename.lastIndexOf("."));
        // 通过UUID.randomUUID()生成随机文件名
        filename = (UUID.randomUUID()+suffix).replace("-", "");
        // 获取时间路径
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        String timePath = sdf.format(new Date());
        // 拼接文件保存路径
        File dirFile = new File(dirPath+timePath);
        // 判断文件路径是否存在，不存在则创建
        if (!dirFile.exists()) dirFile.mkdirs();
        // 拼接图片路径(时间地址路径+文件名)
        String imgPath = timePath+"/"+filename;
        log.debug("{}",dirFile.getName());
        // 保存文件
        file.transferTo(new File(dirPath+imgPath));
        return JsonResult.ok(dirPath.substring(2)+imgPath);
//        log.debug("图片上传控制器:{}",file);
//        String filename = file.getOriginalFilename();
//        String suffix = filename.substring(filename.lastIndexOf("."));
//
//        Date now = new Date();
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd/");
//        String timePath = sdf.format(now);
//
//        filename = (timePath+ UUID.randomUUID()+suffix).replace("-", "");
//        File dirFile = new File(imgPath+timePath);
//        if (!dirFile.exists()) dirFile.mkdirs();
//        file.transferTo(new File(imgPath+filename));
//
//        //spring.web.resources.static-locations=file:static,classpath:static
//        log.debug("{}",imgPath+filename);
////        System.out.println("/profile/upload/"+filename);
//        return JsonResult.ok("/profile/upload/"+filename);
    }

    @ApiIgnore
    @PostMapping("delete")
    public JsonResult delete(String imgPath){
        log.debug("删除图片控制器:{}",imgPath);
        new File(dirPath+imgPath).delete();
        return JsonResult.ok();
    }
}


















